package com.atguigu.dga.governance.assessor.calc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 是否长期无产出
 * @Author: lay
 * @Date: 2024/6/28 18:56
 */
@Component("TABLE_NO_PRODUCE")
public class TableNoProduceAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        //获取参数
        String paramsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSON.parseObject(paramsJson);
        Integer days = jsonObject.getInteger("days");

        //最后修改时间
        Date tableLastModifyTime = assessParam.getTableMetaInfo().getTableLastModifyTime();
        Date tableLastModifyDate = DateUtils.truncate(tableLastModifyTime, Calendar.DATE);
        Date assessDate = DateUtils.parseDate(assessParam.getAssessDate(), "yyyy-MM-dd");
        long dateDiff = Math.abs(assessDate.getTime() - tableLastModifyDate.getTime());
        long dateDiffDays = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);

        if (dateDiffDays > days){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("长期无产出");
        }
        governanceAssessDetail.setAssessComment("实际无产出" + dateDiffDays + "天");
    }
}
